package com.lemeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.lemeng.dao.MenuMapper;
import com.lemeng.entity.PageResult;
import com.lemeng.pojo.Menu;
import com.lemeng.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenuMapper menuMapper;

    @Override
    public List<Menu> findAll() {
        return menuMapper.selectAll();
    }

    @Override
    public PageResult<Menu> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        PageInfo<Menu> menuPageInfo = new PageInfo<>(menuMapper.selectAll());
//        Page<Menu> menus = (Page<Menu>)menuMapper.selectAll();
        return new PageResult(menuPageInfo.getTotal(),menuPageInfo.getList());
    }

    @Override
    public List<Menu> findList(Map<String, Object> serchMap) {
        Example example = new Example(Menu.class);
        Example.Criteria criteria = example.createCriteria();
        if (serchMap != null) {
            if (serchMap.get("name")!=null && StringUtil.isNotEmpty(serchMap.get("name").toString())){
                criteria.andLike("name","%"+serchMap.get("name").toString()+"%");
            }
            if (serchMap.get("letter")!=null && StringUtil.isNotEmpty(serchMap.get("letter").toString())){
                criteria.andEqualTo("letter",serchMap.get("letter").toString());
            }
        }
        return menuMapper.selectByExample(example);
    }

    @Override
    public PageResult<Menu> findPage(Integer page, Integer size, Map<String, Object> serchMap) {
        PageHelper.startPage(page,size);
        PageInfo<Menu> menuPageInfo = new PageInfo<>(findList(serchMap));
        return new PageResult(menuPageInfo.getTotal(),menuPageInfo.getList());
    }

    @Override
    public Menu findById(Integer id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Menu menu) {
        menuMapper.insert(menu);
    }

    @Override
    public void update(Menu menu) {
        menuMapper.updateByPrimaryKeySelective(menu);
    }

    @Override
    public void delete(Integer id) {
        menuMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Map<String, Object> findAllMenus() {
        List<Map<String,Object>> list = new ArrayList<>();//data后边的list
        List<Menu> menus = menuMapper.selectAll();
        list = findMenuByPid(menus,"0");
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("data",list);
        return hashMap;
    }

    public List<Map<String,Object>> findMenuByPid(List<Menu> menus,String pid){
        List<Map<String,Object>> list = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId().equals(pid)){
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("path",menu.getUrl());
                hashMap.put("title",menu.getName());
                hashMap.put("icon",menu.getIcon());
                List<Map<String, Object>> children = findMenuByPid(menus, menu.getId());
                if (children!=null&&children.size()>0){
                    hashMap.put("children",children);
                }
                list.add(hashMap);
            }
        }
        return list;

    }


}
